/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.controller;

import jade.core.AID;
import ca.usherbrooke.agent.BasicAgent;
import ca.usherbrooke.behaviours.controller.request.SubscriberEntity;
import ca.usherbrooke.behaviours.controller.request.SubscriberGoal;
import ca.usherbrooke.goal.EntityGoal;
import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.view.slick.jade.IWorldProvider;

/**
 * @author Cody Stoutenburg
 * 
 */
public abstract class EntityUpdaterAgent extends BasicAgent implements
		IWorldProvider {
	private static final long serialVersionUID = 1L;
	private Long entityGuid;
	private IWorldModel world;
	private EntityGoal goals;

	protected AID worldAgent;
	protected AID goalAgent;

	private SubscriberEntity subscribWorld;
	private SubscriberGoal subscribGoal;

	@Override
	protected void setup() {
		super.setup();

		// receive the aid in args
		worldAgent = (AID) getArguments()[argumentsIdx + 0];
		goalAgent = (AID) getArguments()[argumentsIdx + 1];
		entityGuid = (Long) getArguments()[argumentsIdx + 2];

		argumentsIdx = 3;
		world = null;
		goals = null;

		subscribWorld = new SubscriberEntity(this, worldAgent, entityGuid);
		subscribGoal = new SubscriberGoal(this, goalAgent, entityGuid);

		this.addBehaviour(subscribWorld);
		this.addBehaviour(subscribGoal);
	}

	public void setWorld(IWorldModel world) {
		if (world == null) {
			this.doDelete();
		}
		if (this.world == null && this.goals != null) {
			this.world = world;
			initEntity();
		}
		this.world = world;
	}

	public void setEntityGoals(EntityGoal goals) {
		if (this.world != null && this.goals == null) {
			this.goals = goals;
			initEntity();
		}
		this.goals = goals;
	}

	public EntityGoal getGoals() {
		return this.goals;
	}

	public AID getWorldAgent() {
		return this.worldAgent;
	}

	public Long getEntityGUID() {
		return this.entityGuid;
	}

	public IEntityModel getEntity() {
		IEntityModel result = null;
		if (world != null) {
			result = this.world.getEntity(entityGuid);
		}
		return result;
	}

	@Override
	public IWorldModel getWorld() {
		return this.world;
	}

	public abstract void initEntity();

	@Override
	protected void takeDown() {
		super.takeDown();
		subscribWorld.unsubscribe();
	}
}
